﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace Crm.Views
{
    public enum ListeTipi
    {
        CariSec,
        AdayCariTanitim,
        YeniGorusme,
        GecmisGorusme,
        YeniRandevu,
        GecmisRandevu,
        Kampanyalar,
        FirmaDurumTanim,
        FirmaTipi,
        GorusmeSekil,
        KapamaTanim,
        DepartmanTanim,
        GorevTanim,
        IslemMerkezi,
        KullaniciParametre,
        AdayCariYonetim,
        RandevuTipi,
        KampanyaListesi,
        Anketler
    }

    public partial class Liste : Page
    {
        CrmClient.CrmServiceClient client = new CrmClient.CrmServiceClient();
        private ListeTipi listeTipi;

        public Liste()
        {
            InitializeComponent();
            client.TanimListeleCompleted += client_TanimListeleCompleted;
            client.TanimSilCompleted += client_TanimSilCompleted;
            client.FirmaListesiCompleted += client_CariListesiCompleted;
            client.GorusmelerListeleCompleted += client_GorusmelerListeleCompleted;
            client.RandevuListeleCompleted += client_RandevuListeleCompleted;
            client.AdayCariListesiCompleted += client_AdayCariListesiCompleted;
            client.AdayCariSilCompleted += client_AdayCariSilCompleted;
            this.Loaded += Liste_Loaded;
            client.AnketListesiCompleted += client_AnketListesiCompleted;
            client.KampanyaListesiCompleted += client_KampanyaListesiCompleted;
            client.KampanyaTanimSilCompleted += client_KampanyaTanimSilCompleted;
            client.AnketSilCompleted += client_AnketSilCompleted;
        }

        void client_AnketSilCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            client.AnketListesiAsync();
        }

        void client_KampanyaTanimSilCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                Listele();
            }
        }

        void client_KampanyaListesiCompleted(object sender, CrmClient.KampanyaListesiCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                grdListe.DataSource = null;
                grdListe.DataSource = e.Result;

                grdListe.Columns["KampanyaAdi"].VisibleIndex = 1;
                grdListe.Columns["KampanyaAdi"].HeaderContent = "Kampanya Tanımı";

                grdListe.Columns["No"].VisibleIndex = 0;
                grdListe.Columns["No"].HeaderContent = "Kampanya No";

                grdListe.Columns["FirmaKodu"].VisibleIndex = 2;
                grdListe.Columns["FirmaKodu"].HeaderContent = "Firma Kodu";

                grdListe.Columns["FirmaUnvan"].HeaderContent = "Firma Ünvanı";
                grdListe.Columns["FirmaUnvan"].VisibleIndex = 3;

                grdListe.Columns["ID"].Visible = false;
                grdListe.Columns["KampanyaTipi"].Visible = false;
                grdListe.Columns["GridList"].Visible = false;
            }
        }

        void client_AnketListesiCompleted(object sender, CrmClient.AnketListesiCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                grdListe.DataSource = null;
                grdListe.DataSource = e.Result;

                grdListe.Columns["ID"].Visible = false;
                grdListe.Columns["Sorular"].Visible = false;
                grdListe.Columns["AnketAdi"].HeaderContent = "Anket Tanımı";
                grdListe.Columns["AnketAdi"].VisibleIndex = 0;
                grdListe.Columns["Gecerli"].HeaderContent = "Geçerli";
                grdListe.Columns["Gecerli"].VisibleIndex = 1;
            }
        }

        void client_AdayCariSilCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                Listele();
            }
        }

        void client_AdayCariListesiCompleted(object sender, CrmClient.AdayCariListesiCompletedEventArgs e)
        {
            grdListe.DataSource = null;
            grdListe.DataSource = e.Result;

            grdListe.Columns["Kodu"].HeaderContent = "Aday Cari Kodu";
            grdListe.Columns["Kodu"].VisibleIndex = 0;

            grdListe.Columns["Unvan1"].HeaderContent = "Aday Cari Ünvanı";
            grdListe.Columns["Unvan1"].VisibleIndex = 1;

            grdListe.Columns["Unvan2"].HeaderContent = "Aday Cari Ünvanı";
            grdListe.Columns["Unvan2"].VisibleIndex = 2;

            grdListe.Columns["TemsilciAdi"].HeaderContent = "Temsilci Adı";
            grdListe.Columns["TemsilciAdi"].VisibleIndex = 3;

            grdListe.Columns["GrupAdi"].HeaderContent = "Grup Adı";
            grdListe.Columns["GrupAdi"].VisibleIndex = 4;

            grdListe.Columns["SektorAdi"].HeaderContent = "Sektör Adı";
            grdListe.Columns["SektorAdi"].VisibleIndex = 5;

            grdListe.Columns["BolgeAdi"].VisibleIndex = 3;
            grdListe.Columns["BolgeAdi"].HeaderContent = "Bölge Adı";
            grdListe.Refresh();
        }


        void client_RandevuListeleCompleted(object sender, CrmClient.RandevuListeleCompletedEventArgs e)
        {
            grdListe.DataSource = e.Result;
            for (int i = 0; i < grdListe.Columns.Count; i++)
            {
                grdListe.Columns[i].Visible = false;
            }

            grdListe.Columns["BaslangicTarihi"].HeaderContent = "Randevu Başlangıç Tarihi";
            grdListe.Columns["BaslangicTarihi"].VisibleIndex = 0;
            grdListe.Columns["FirmaTipAdi"].HeaderContent = "Hesap Tipi";
            grdListe.Columns["FirmaTipAdi"].VisibleIndex = 1;
            grdListe.Columns["FirmaUnvani"].HeaderContent = "Hesap Adı";
            grdListe.Columns["FirmaUnvani"].VisibleIndex = 2;
            grdListe.Columns["Konu"].HeaderContent = "Randevu Konusu";
            grdListe.Columns["Konu"].VisibleIndex = 3;
            grdListe.Columns["Yer"].HeaderContent = "Randevu Yeri";
            grdListe.Columns["Yer"].VisibleIndex = 4;
            grdListe.Columns["Detay"].HeaderContent = "Detaylar";
            grdListe.Columns["Detay"].VisibleIndex = 5;
        }

        void client_GorusmelerListeleCompleted(object sender, CrmClient.GorusmelerListeleCompletedEventArgs e)
        {
            grdListe.DataSource = e.Result;
            for (int i = 0; i < grdListe.Columns.Count; i++)
            {
                grdListe.Columns[i].Visible = false;
            }

            grdListe.Columns["BaslangicTarihi"].HeaderContent = "Görüşme Başlangıç Tarihi";
            grdListe.Columns["BaslangicTarihi"].VisibleIndex = 0;
            grdListe.Columns["FirmaTipAdi"].HeaderContent = "Hesap Tipi";
            grdListe.Columns["FirmaTipAdi"].VisibleIndex = 1;
            grdListe.Columns["FirmaUnvani"].HeaderContent = "Hesap Adı";
            grdListe.Columns["FirmaUnvani"].VisibleIndex = 2;
            grdListe.Columns["Konu"].HeaderContent = "Görüşme Konusu";
            grdListe.Columns["Konu"].VisibleIndex = 3;
            grdListe.Columns["Yer"].HeaderContent = "Görüşme Yeri";
            grdListe.Columns["Yer"].VisibleIndex = 4;
            grdListe.Columns["Detay"].HeaderContent = "Detaylar";
            grdListe.Columns["Detay"].VisibleIndex = 5;
            grdListe.Columns["BitisTarihi"].HeaderContent = "Görüşme Bitiş Tarihi";
            grdListe.Columns["BitisTarihi"].VisibleIndex = 6;

        }

        void client_CariListesiCompleted(object sender, CrmClient.FirmaListesiCompletedEventArgs e)
        {
            grdListe.DataSource = e.Result;
            grdListe.Columns["Adi"].HeaderContent = "Firma Ünvanı";
            grdListe.Columns["ID"].Visible = false;
            grdListe.Columns["Kodu"].HeaderContent = "Firma Kodu";
            grdListe.Columns["Kodu"].VisibleIndex = 0;
            grdListe.Columns["Adi"].VisibleIndex = 1;
            grdListe.Columns["HesapTipi"].Visible = false;
        }

        void client_TanimSilCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {

        }

        void Liste_Loaded(object sender, RoutedEventArgs e)
        {
            Listele();
        }

        private void Listele()
        {
            switch (listeTipi)
            {
                case ListeTipi.CariSec:
                    client.FirmaListesiAsync("%", true, 0);
                    break;
                case ListeTipi.YeniGorusme:
                    break;
                case ListeTipi.GecmisGorusme:
                    client.GorusmelerListeleAsync();
                    break;
                case ListeTipi.YeniRandevu:
                    break;
                case ListeTipi.GecmisRandevu:
                    client.RandevuListeleAsync();
                    break;
                case ListeTipi.Kampanyalar:
                    break;
                case ListeTipi.RandevuTipi:
                case ListeTipi.FirmaDurumTanim:
                case ListeTipi.FirmaTipi:
                case ListeTipi.GorusmeSekil:
                case ListeTipi.KapamaTanim:
                case ListeTipi.DepartmanTanim:
                case ListeTipi.GorevTanim:
                case ListeTipi.IslemMerkezi:
                case ListeTipi.KullaniciParametre:
                    client.TanimListeleAsync((byte)listeTipi);
                    break;
                case ListeTipi.AdayCariYonetim:
                    client.AdayCariListesiAsync();
                    break;
                case ListeTipi.Anketler:
                    client.AnketListesiAsync();
                    break;
                case ListeTipi.KampanyaListesi:
                    client.KampanyaListesiAsync();
                    break;

                default:
                    break;
            }
        }

        void client_TanimListeleCompleted(object sender, CrmClient.TanimListeleCompletedEventArgs e)
        {
            grdListe.DataSource = e.Result;
            switch (listeTipi)
            {
                case ListeTipi.RandevuTipi:
                case ListeTipi.FirmaDurumTanim:
                case ListeTipi.FirmaTipi:
                case ListeTipi.GorusmeSekil:
                case ListeTipi.KapamaTanim:
                case ListeTipi.DepartmanTanim:
                case ListeTipi.GorevTanim:
                case ListeTipi.IslemMerkezi:
                case ListeTipi.KullaniciParametre:
                case ListeTipi.AdayCariYonetim:
                    grdListe.Columns[0].HeaderContent = "Açıklama";
                    grdListe.Columns[1].Visible = false;
                    grdListe.Columns[2].Visible = false;
                    grdListe.Columns[3].Visible = false;
                    break;
                default:
                    break;
            }
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (this.NavigationContext.QueryString.ContainsKey("tip"))
            {
                listeTipi = (ListeTipi)Convert.ToByte(this.NavigationContext.QueryString["tip"]);
            }
        }

        private void btnYeni(object sender, EventArgs e)
        {
            switch (listeTipi)
            {
                case ListeTipi.CariSec:
                    break;
                case ListeTipi.AdayCariTanitim:
                    break;
                case ListeTipi.GecmisGorusme:
                    Gorusme grs = new Gorusme();
                    grs.EskiEvrak = false;
                    grs.GorusmeEvragi = true;
                    grs.Closed += frm_Closed;
                    grs.Show();
                    break;
                case ListeTipi.GecmisRandevu:
                    Gorusme rnd = new Gorusme();
                    rnd.EskiEvrak = false;
                    rnd.GorusmeEvragi = false;
                    rnd.Closed += frm_Closed;
                    rnd.Show();
                    break;
                case ListeTipi.Kampanyalar:
                    break;
                case ListeTipi.RandevuTipi:
                case ListeTipi.FirmaDurumTanim:
                case ListeTipi.FirmaTipi:
                case ListeTipi.GorusmeSekil:
                case ListeTipi.KapamaTanim:
                case ListeTipi.DepartmanTanim:
                case ListeTipi.GorevTanim:
                case ListeTipi.IslemMerkezi:
                case ListeTipi.KullaniciParametre:
                    PrmTanim frm = new PrmTanim();
                    frm.FormTipi = listeTipi;
                    frm.Closed += frm_Closed;
                    frm.Show();
                    break;
                case ListeTipi.AdayCariYonetim:
                    AdayCariKarti ady = new AdayCariKarti();
                    ady.Closed += frm_Closed;
                    ady.Show();
                    break;
                case ListeTipi.Anketler:
                    AddSurvey sr = new AddSurvey();
                    sr.EskiEvrak = false;
                    sr.Closed += frm_Closed;
                    sr.Show();
                    break;
                case ListeTipi.KampanyaListesi:
                    Kampanya kp = new Kampanya();
                    kp.EskiEvrak = false;
                    kp.Closed += frm_Closed;
                    kp.Show();
                    break;
                default:
                    break;
            }
        }

        void frm_Closed(object sender, EventArgs e)
        {
            if (((ChildWindow)sender).DialogResult == true)
            {
                Listele();
            }
        }

        private void btnSil(object sender, EventArgs e)
        {
            switch (listeTipi)
            {
                case ListeTipi.CariSec:
                    break;
                case ListeTipi.AdayCariTanitim:
                    break;
                case ListeTipi.YeniGorusme:
                    break;
                case ListeTipi.GecmisGorusme:
                    break;
                case ListeTipi.YeniRandevu:
                    break;
                case ListeTipi.GecmisRandevu:
                    break;
                case ListeTipi.Kampanyalar:
                    break;
                case ListeTipi.RandevuTipi:
                case ListeTipi.FirmaDurumTanim:
                case ListeTipi.FirmaTipi:
                case ListeTipi.GorusmeSekil:
                case ListeTipi.KapamaTanim:
                case ListeTipi.DepartmanTanim:
                case ListeTipi.GorevTanim:
                case ListeTipi.IslemMerkezi:
                case ListeTipi.KullaniciParametre:
                    if (grdListe.FocusedRowHandle > -1)
                    {
                        CrmClient.GenelTanim tnm = (CrmClient.GenelTanim)grdListe.GetDataRow(grdListe.FocusedRowHandle);
                        client.TanimSilAsync(tnm);
                        client.TanimListeleAsync((byte)listeTipi);
                    }
                    break;
                case ListeTipi.AdayCariYonetim:
                    if (grdListe.FocusedRowHandle > -1)
                    {
                        Crm.CrmClient.AdayCariOzet tnm = (Crm.CrmClient.AdayCariOzet)grdListe.GetDataRow(grdListe.FocusedRowHandle);
                        client.AdayCariSilAsync(tnm.Kodu);
                    }
                    break;
                case ListeTipi.KampanyaListesi:
                    if (grdListe.FocusedRowHandle > -1)
                    {
                        Crm.CrmClient.KampanyaTanimlari tnm = (Crm.CrmClient.KampanyaTanimlari)grdListe.GetDataRow(grdListe.FocusedRowHandle);
                        client.KampanyaTanimSilAsync(tnm.No);
                    }
                    break;
                case ListeTipi.Anketler:
                    if (grdListe.FocusedRowHandle > -1)
                    {
                        Crm.CrmClient.AnketTanimlari tnm = (Crm.CrmClient.AnketTanimlari)grdListe.GetDataRow(grdListe.FocusedRowHandle);
                        client.AnketSilAsync(tnm.ID);
                    }
                    break;
                default:
                    break;
            }
        }

        private void btnDegistir(object sender, EventArgs e)
        {
            switch (listeTipi)
            {
                case ListeTipi.CariSec:
                    break;
                case ListeTipi.AdayCariTanitim:
                    break;
                case ListeTipi.GecmisGorusme:
                    if (grdListe.FocusedRowHandle > -1)
                    {
                        Gorusme gr = new Gorusme();
                        gr.Hareket = (CrmClient.Hareket)grdListe.GetDataRow(grdListe.FocusedRowHandle);
                        gr.EskiEvrak = true;
                        gr.Closed += frm_Closed;
                        gr.GorusmeEvragi = true;
                        gr.Show();
                    }
                    break;
                case ListeTipi.GecmisRandevu:
                    if (grdListe.FocusedRowHandle > -1)
                    {
                        Gorusme gr = new Gorusme();
                        gr.Hareket = (CrmClient.Hareket)grdListe.GetDataRow(grdListe.FocusedRowHandle);
                        gr.EskiEvrak = true;
                        gr.Closed += frm_Closed;
                        gr.GorusmeEvragi = false;
                        gr.Show();
                    }
                    break;
                case ListeTipi.Kampanyalar:
                    break;
                case ListeTipi.RandevuTipi:
                case ListeTipi.FirmaDurumTanim:
                case ListeTipi.FirmaTipi:
                case ListeTipi.GorusmeSekil:
                case ListeTipi.KapamaTanim:
                case ListeTipi.DepartmanTanim:
                case ListeTipi.GorevTanim:
                case ListeTipi.IslemMerkezi:
                case ListeTipi.KullaniciParametre:
                    if (grdListe.FocusedRowHandle > -1)
                    {
                        PrmTanim frm = new PrmTanim();
                        frm.Data = (CrmClient.GenelTanim)grdListe.GetDataRow(grdListe.FocusedRowHandle);
                        frm.FormTipi = listeTipi;
                        frm.Closed += frm_Closed;
                        frm.Show();
                    }
                    break;
                case ListeTipi.AdayCariYonetim:
                    if (grdListe.FocusedRowHandle > -1)
                    {
                        AdayCariKarti aday = new AdayCariKarti();
                        Crm.CrmClient.AdayCariOzet tnm = (Crm.CrmClient.AdayCariOzet)grdListe.GetDataRow(grdListe.FocusedRowHandle);
                        aday.CariKodu = tnm.Kodu;
                        aday.EskiEvrak = true;
                        aday.Closed += frm_Closed;
                        aday.Show();
                    }
                    break;
                case ListeTipi.Anketler:
                    if (grdListe.FocusedRowHandle > -1)
                    {
                        Crm.CrmClient.AnketTanimlari tnm = (Crm.CrmClient.AnketTanimlari)grdListe.GetDataRow(grdListe.FocusedRowHandle);
                        AddSurvey sr = new AddSurvey();
                        sr.AnketID = tnm.ID;
                        sr.EskiEvrak = true;
                        sr.Closed += frm_Closed;
                        sr.Show();
                    }
                    break;
                case ListeTipi.KampanyaListesi:
                    if (grdListe.FocusedRowHandle > -1)
                    {
                        Crm.CrmClient.KampanyaTanimlari tnm = (Crm.CrmClient.KampanyaTanimlari)grdListe.GetDataRow(grdListe.FocusedRowHandle);
                        Kampanya kp = new Kampanya();
                        kp.KampanyaID = tnm.No;
                        kp.EskiEvrak = true;
                        kp.Closed += frm_Closed;
                        kp.Show();
                    }
                    break;
                default:
                    break;
            }
        }

    }
}
